<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <!-- <input type="text" @input="handleInput"> -->
    <input type="text" v-model="todo">
    <button @click="handleClick()">add</button>
    <div v-if="todolist.length === 0">代办事项空空</div>
    <ul v-else>
      <li v-for="(item, index) of todolist" :key="item.id">
        {{ index + 1}} --- {{ item.todo }}
        <button @click="handleDel(index)">del</button>
      </li>
    </ul>
    <hr>
    <div :class="classObj">动态切换class-对象写法</div>
    <div :class="classArr">动态切换class-数组写法</div>
    <div :style="styleObj">动态切换style-对象写法</div>
    <div :style="styleArr">动态切换style-数组写法</div>
    <hr>
    <input type="text" v-model="search" @input="handleSearch">
    <ul>
      <li v-for="item in dataList">{{ item }}</li>
    </ul>
    {{ myMethod() }}

    <input type="text" @keyup.13="handleKeyUp">
    <input type="text" @keyup="handleKeyUp">
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    var vm = new Vue({
      el: '#app',
      data: {
        todo: '',
        todolist: [],
        classObj: {
          aa: true,
          bb: true,
          cc: true
        }, // 可以直接改，但是不能直接添加dd，Vue.set(vm.classObj, 'dd' , true) // 拦截
        classArr: ['aa', 'bb', 'cc'], // 可直接添加dd
        styleObj: {
          backgroundColor: "red"
        }, // Vue.set(vm.classObj, 'fontSize' , "40px") // 拦截
        styleArr: [{
          backgroundColor: "red"
        }],
        search: '',
        dataList: ['aaa', 'bbb', 'ccc', 'ddd', 'abb', 'cdd'],
        originList: ['aaa', 'bbb', 'ccc', 'ddd', 'abb', 'cdd'],
      },
      methods: {
        handleClick() {
          if(!this.todo) return
          var temp = {}
          temp.todo = this.todo
          temp.id = new Date().getTime()
          this.todolist.unshift(temp)
          // 重置input
          this.todo = ''
        },
        handleInput(evt) {
          console.log(evt)
          console.log('input', evt.target.value)
          this.todo = evt.target.value
        },
        handleDel(index) {
          this.todolist.splice(index, 1)
        },
        handleSearch() { // 模糊查询01 过滤应用
          // 过滤数组filter
          // 原始数据进行过滤，再赋值
          this.dataList = this.originList.filter(item => item.includes(this.search))
        },
        myMethod() { // 模糊查询02 过滤应用-函数表达式
          //依赖此状态的函数也会重新执行
          return this.dataList.filter(item => item.includes(this.search))
        },
        handleKeyUp(evt) {
          console.log(evt)
          // if(evt.keyCode === 13) {
          //   console.log('回车')
          // }
        }
      }
    })

    var obj = {}
    Object.defineProperty(obj, 'myname', {
      set() {
        console.log('有人修改了')
      },
      get() {
        console.log('有人访问了')
      }
    })
    // obj.myname = 'xxx'

    var arr = [
      { a: 1 },
      { b: 2 },
      {
        c: {
          d: 4
        }
      }
    ]
    /**
   * Mix properties into target object.
   */
    function extend(to, _from) {
      // {}  {a:1} {b:2}
      for (var key in _from) {
        // a, b
        to[key] = _from[key];
        { a: 1 }
        return to
      }
    }

    /**
     * Merge an Array of Objects into a single Object.
     */
    function toObject(arr) {
      var res = {};
      for (var i = 0; i < arr.length; i++) {
        if (arr[i]) { // {a:1} {b:2}
          extend(res, arr[i]);
        }
      }
      return res
    }
    console.log(toObject(arr)) 
  </script>
</body>

</html>